using System;
using System.Collections.Generic;
using System.Text;
using SharpMud.Net.Telnet.ANSI;
using SharpMud.Text.Formatting;
using SharpMud.Text.Formatting.BuiltIn;

namespace SharpMud.Net.Telnet.Processors.TextFormatting
{
	public class GlobalTokenizationList
	{
        private static FormatTokenizationList   _TokenizationList;
        private static bool _UseDefault;
        private static FormatTokenizationList   _Default;

        private GlobalTokenizationList()
        {
        }

        static GlobalTokenizationList()
        {
            _UseDefault = true;
            _Default = new FormatTokenizationList(
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Black),      "[black]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Blue),       "[blue]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Cyan),       "[cyan]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Cyan),       "[teal]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Green),      "[green]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Magenta),    "[magenta]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Magenta),    "[purple]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Red),        "[red]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.White),      "[white]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Yellow),     "[yellow]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Default),    "[default]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.DarkGray),   "[darkgray]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.DarkBlue),   "[darkblue]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.DarkCyan),   "[darkcyan]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.DarkCyan),   "[darkteal]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.DarkGreen),  "[darkgreen]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.DarkMagenta),"[darkmagenta]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.DarkMagenta),"[darkpurple]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.DarkRed),    "[darkred]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Gray),       "[gray]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Gray),       "[grey]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.DarkYellow), "[darkyellow]"),
                FormatTokenization.MakeExactTokenization(typeof(SetForegroundColor.Default),    "[default]"),

                FormatTokenization.MakeExactTokenization( typeof(SetBackgroundColor.Black),     "[#black]"  ),
			    FormatTokenization.MakeExactTokenization( typeof(SetBackgroundColor.Blue),      "[#blue]"   ),
			    FormatTokenization.MakeExactTokenization( typeof(SetBackgroundColor.Cyan),      "[#cyan]"   ),
                FormatTokenization.MakeExactTokenization( typeof(SetBackgroundColor.Cyan),      "[#teal]"),
			    FormatTokenization.MakeExactTokenization( typeof(SetBackgroundColor.Green),     "[#green]"  ),
			    FormatTokenization.MakeExactTokenization( typeof(SetBackgroundColor.Magenta),   "[#magenta]"),
                FormatTokenization.MakeExactTokenization(typeof(SetBackgroundColor.Magenta),    "[#purple]"),
			    FormatTokenization.MakeExactTokenization( typeof(SetBackgroundColor.Red),       "[#red]"    ),
			    FormatTokenization.MakeExactTokenization( typeof(SetBackgroundColor.Gray),      "[#gray]"   ),
                FormatTokenization.MakeExactTokenization( typeof(SetBackgroundColor.Gray),      "[#grey]"),
			    FormatTokenization.MakeExactTokenization( typeof(SetBackgroundColor.Yellow),    "[#yellow]" ),
			    FormatTokenization.MakeExactTokenization( typeof(SetBackgroundColor.Default),   "[#default]"),

                FormatTokenization.MakeExactTokenization( typeof(ExactTextInsertion.LeftSquareBracket),   "^["),
                FormatTokenization.MakeExactTokenization( typeof(ExactTextInsertion.RightSquareBracket),  "^]"),
                FormatTokenization.MakeExactTokenization( typeof(ExactTextInsertion.LeftCurlyBrace),      "^{"),
                FormatTokenization.MakeExactTokenization( typeof(ExactTextInsertion.RightCurlyBrace),     "^}"),
                FormatTokenization.MakeExactTokenization( typeof(ExactTextInsertion.SessionNewLine),      "^!"),
                FormatTokenization.MakeExactTokenization( typeof(ExactTextInsertion.SystemNewLine),       "^@"),
                FormatTokenization.MakeExactTokenization( typeof(SetTextNormal),                    "^."),
                FormatTokenization.MakeExactTokenization( typeof(SetTextBold),                      "^b"),
                FormatTokenization.MakeExactTokenization( typeof(SetTextUnderline),                 "^u"),
                FormatTokenization.MakeExactTokenization( typeof(SetTextItalic),                    "^i"),
                FormatTokenization.MakeExactTokenization( typeof(SetTextReverseColors),             "^%"),
                FormatTokenization.MakeExactTokenization( typeof(SetTextInvisible),                 "^?"),
                FormatTokenization.MakeExactTokenization( typeof(SetTextBlink),                     "^|")
             );
        }

        public static FormatTokenizationList TokenizationList
        {
            get
            {
                if (_TokenizationList == null || _UseDefault == true)
                    return _Default;
                else
                    return _TokenizationList;
            }
            set
            {
                lock (_TokenizationList)
                {
                    lock (_Default)
                    {

                        if (_TokenizationList == null && value != null)
                            _UseDefault = false;
                        _TokenizationList = value;
                    }
                }
            }
        }

        public static bool UseDefault
        {
            get
            {
                if (_TokenizationList == null)
                    return true;
                else
                    return _UseDefault;
            }
            set
            {
                if (value == false && _TokenizationList == null)
                {
                    throw new InvalidOperationException("Cannot set to use a non-default global TokenizationList without assigning the TokenizationList first.");
                }
                _UseDefault = value;
            }
        }
	}
}